package com.podium.auth.service.impl;

import com.podium.auth.entity.LoginUser;
import com.podium.web.system.entity.SysUser;
import com.podium.web.system.service.SysUserService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @author 邵文泽
 * @description 用户信息处理
 * @date 2025/4/15 8:46
 */
@Slf4j
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Resource
    private SysUserService userService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        LoginUser loginUser = new LoginUser();
        SysUser sysUser = userService.findByUserName(username);

        if (sysUser == null || sysUser.getId() == null)
        {
            log.warn("登录用户：{} 不存在.", username);
            throw new RuntimeException("账号或密码错误！");
        }

        loginUser.setUsername(sysUser.getUsername());
        loginUser.setPassword(sysUser.getPassword());
        loginUser.setUserId(sysUser.getId());
        sysUser.setPassword(null);
        loginUser.setUser(sysUser);
        return loginUser;
    }
}
